package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.entity.BusinessData;
import com.itheima.health.entity.HotSetmealData;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.SetmealCount;
import com.itheima.health.pojo.SetmealDto;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 数据报表service
 */
@Service
@Slf4j
@SuppressWarnings("all")
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    @Override
    public SetmealDto getSetmealReport() {
        //创建一个返回对象
        SetmealDto setmealDto = new SetmealDto();
        //调用dao层进行查询想要的数据
        List<SetmealCount> setmealCounts = orderDao.findCounts();
        List<String> names = orderDao.findNames();
        //给dao对象设置上属性
        setmealDto.setSetmealCount(setmealCounts);
        setmealDto.setSetmealNames(names);


        //查询成功返回对象
        return setmealDto;
    }

    @Override
    public BusinessData getBusinessReportData() {
       //查询到需要的数据
                                                                                                                                           //查询会员信息
        Integer todayNewMember = memberDao.todayNewMember();
        Integer weekNewMember = memberDao.thisWeekNewMember();
        Integer monthNewMember = memberDao.thisMonthNewMember();
        Integer totalMember = memberDao.totalMember();
        //查询订单信息
        Integer todayOrderNumber = orderDao.todayOrderNumber();
        Integer thisWeekOrderNumber = orderDao.thisWeekOrderNumber();
        Integer thisMonthOrderNumber = orderDao.thisMonthOrderNumber();
        Integer todayVisitsNumber = orderDao.todayVisitsNumber();
        Integer thisWeekVisitsNumber = orderDao.thisWeekVisitsNumber();
        Integer thisMonthVisitsNumber = orderDao.thisMonthVisitsNumber();
        //查询套餐数据
        List<HotSetmealData> hotSetmealDatas = orderDao.hotSetmeal();
        for (HotSetmealData hotSetmealData : hotSetmealDatas) {
            hotSetmealData.setProportion(hotSetmealData.getSetmeal_count().divide(orderDao.totalOrderNumber(),4, BigDecimal.ROUND_HALF_UP));
        }
        //日期
        String reportDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //将数据放进对象中并返回
        //生成一个接收数据的对象
        BusinessData businessData = new BusinessData(reportDate,todayNewMember,thisWeekOrderNumber,thisMonthOrderNumber,totalMember,todayVisitsNumber,thisWeekVisitsNumber,thisMonthVisitsNumber,todayOrderNumber,thisMonthOrderNumber,thisWeekOrderNumber,hotSetmealDatas);


        return businessData;

    }
    }


